package com.hetao.xx.activity;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;

import com.google.android.material.tabs.TabLayout;
import com.hetao.xx.Global;
import com.hetao.xx.constants;
import com.hetao.xx.fragment.HoleFragment;
import com.hetao.xx.fragment.HomeFragment;
import com.hetao.xx.fragment.MessageFragment;
import com.hetao.xx.fragment.MineFragment;
import com.hetao.xx.R;
import com.hetao.xx.adapter.AgentTabAdapter;
import com.hetao.xx.orm.PlaceVo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;

import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.hetao.xx.constants.MainNotifyMsg;

public class MainActivity extends AppCompatActivity {

    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.tl_main)
    TabLayout tabLayout;
    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.vp_main)
    ViewPager viewPager;
    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.main_msg)
    LinearLayout msgLayout;

    public Handler mHandler = new Handler(Looper.getMainLooper()) {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case constants.MainNotifyMsg:
                    String text = (String)msg.obj;
                    addTextView(text);
                    break;

                case constants.MainSwitchPage:
                    homeFragment.switchFragment(msg.arg1, msg.arg2, msg.obj);
                    break;

                case constants.MainUpdateSite:
                    String site = (String)msg.obj;
                    homeFragment.updateSite(site);
                    break;

                case constants.MainUpdateMine:
                    mineFragment.refreshUserInfo();
                    break;
            }
        }
    };

    private HomeFragment homeFragment;
    private MineFragment mineFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        List<Fragment> fragments = new ArrayList<>();
        homeFragment = new HomeFragment();
        mineFragment = new MineFragment();
        fragments.add(homeFragment);
        fragments.add(new HoleFragment());
        fragments.add(mineFragment);
        viewPager.setAdapter(new AgentTabAdapter(getSupportFragmentManager(), fragments));

        loadTabLayout();
        Global.getInstant().setHandler(mHandler);

    }

    private void loadTabLayout() {

        List<String> tabItems = new ArrayList<>(4);
        tabItems.add("探险");
        tabItems.add("洞府");
        tabItems.add("我");
        tabLayout.setupWithViewPager(viewPager);
        int selectPosition = tabLayout.getSelectedTabPosition();
        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            tab.setCustomView(R.layout.widget_tab_item);
            View tabContainer = tab.getCustomView();
            if (i >= tabItems.size()) {
                return;
            }
            TextView tvBottom = tabContainer.findViewById(R.id.tab_text);
            tvBottom.setText(tabItems.get(i));
            if (i == selectPosition) {
                tabContainer.setSelected(true);
            }
        }
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            public void onTabSelected(TabLayout.Tab tab) {
                if (tab.getCustomView() != null) {
                    tab.getCustomView().setSelected(true);
                }
            }

            public void onTabUnselected(TabLayout.Tab tab) {
                if (tab.getCustomView() != null) {
                    tab.getCustomView().setSelected(false);
                }
            }

            public void onTabReselected(TabLayout.Tab tab) {
                if (viewPager.getCurrentItem() == 0) {
                }
            }
        });
    }

    public void addTextView(String msg) {
        final TextView tmp = new TextView(this);
        tmp.setText(msg);
        tmp.setTextColor(Color.WHITE);

        msgLayout.addView(tmp);
        Animation animation = AnimationUtils.loadAnimation(MainActivity.this,R.anim.anim_msg_alpha);
        tmp.startAnimation(animation);

        mHandler.postDelayed(() -> {
            try {
                msgLayout.removeViewInLayout(tmp);
            } catch (Exception ignored) {
            }
        },animation.getDuration());
    }
}